#!/bin/bash

DIRS="cpp_hello dump_toks id_stat \
	getch line log many_keys nest strings \
	tok_remap two_lex yacc"

EXES="cpp_hello/cpp_hello dump_toks/dump_toks id_stat/id_stat \
	getch/getch line/line_by_line log/logging many_keys/many_keys \
	nest/nest strings/strings tok_remap/tok_remap \
	two_lex/two_lex yacc/ulc2xml"

test_home="$PWD"
temp_dir="/tmp"

outfile1="$temp_dir/uls_a1_res.txt"
outfile2="$temp_dir/uls_a2_res.txt"

dfl_file0="input1.txt"
dfl_file1="input1.out"
dfl_file2="input1.2ut"

for f in $EXES; do
	cd $test_home

	prog=`basename $f`
	dir=${f%/${prog}}
	cd $dir

	echo "checking $prog, ..."
	if [ ! -e $prog ]; then
		echo "$prog not exists, continuing, ..."
		continue;
	fi

	if [ -f $dfl_file0 ]; then
		$prog $dfl_file0 > $outfile1 2> $outfile2
	else
		$prog > $outfile1 2> $outfile2
	fi

	if [ $? != 0 ]; then
		echo "fail to exec $prog";
		continue;
	fi

	if [ -f $dfl_file1 ]; then
		diff -q $outfile1 $dfl_file1;
		if [ $? != 0 ]; then
			echo "fail(stdout): diff for '$prog'";
		fi
	fi

	if [ -f $dfl_file2 ]; then
		diff -q $outfile2 $dfl_file2;
		if [ $? != 0 ]; then
			echo "fail(stderr): diff for '$prog'";
		fi
	fi

	rm $outfile1 $outfile2

done

